package com.rabbit.producer.service.impl;

import com.rabbit.producer.constant.BrokerMessageStatus;
import com.rabbit.producer.entity.BrokerMessage;
import com.rabbit.producer.mapper.BrokerMessageMapper;
import com.rabbit.producer.service.MessageStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: 顾志杰
 */
@Service
public class MessageStoreServiceImpl implements MessageStoreService {

    @Autowired
    private BrokerMessageMapper brokerMessageMapper;


    @Override
    public int insert(BrokerMessage brokerMessage){
        int insert = this.brokerMessageMapper.insert(brokerMessage);
        return insert;
    }


    @Override
    public void succuess(String messageId){
        this.brokerMessageMapper.changeBrokerMessageStatus(messageId, BrokerMessageStatus.SEND_OK.getCode(),new Date());
    }

    @Override
    public void failure(String messageId){
        this.brokerMessageMapper.changeBrokerMessageStatus(messageId, BrokerMessageStatus.SEND_FALL.getCode(),new Date());
    }

    @Override
    public List<BrokerMessage> fetchTimeOutMessageForRertry(BrokerMessageStatus brokerMessageStatus) {
        return this.brokerMessageMapper.queryBrokerMessageStatusForTimeout(brokerMessageStatus.getCode());
    }

    @Override
    public BrokerMessage selectByPrimaryKey(String messageId) {
        BrokerMessage brokerMessage = this.brokerMessageMapper.selectByPrimaryKey(messageId);
        return brokerMessage;
    }

    @Override
    public int updateTryCount(String brokerMessageId) {
        int updateCount = this.brokerMessageMapper.update4TryCount(brokerMessageId, new Date());
        return updateCount;
    }
}
